<template>
  <div>
    <el-card style="margin: 20px 0">
      <!-- 三级分类全局组件 -->
      <CategorySelect @getCategoryId="getCategoryId"></CategorySelect>
    </el-card>
    <el-card>
      <!-- 默认页面 -->
      <div v-show="isShow == 1">
        <el-button type="primary" icon="el-icon-plus" @click="addSpu"
          >添加spu</el-button
        >
        <el-table :data="records" style="width: 100%; margin: 20px 0" border>
          <!-- 第一列 -->
          <el-table-column label="序号" width="80" align="center" type="index">
          </el-table-column>
          <!-- 第二列 -->
          <el-table-column prop="spuName" label="spu名称" width="width">
          </el-table-column>
          <!-- 第三列 -->
          <el-table-column prop="description" label="spu描述" width="width">
          </el-table-column>
          <!-- 第四列 -->
          <el-table-column label="操作" width="width">
            <template slot-scope="{ row }">
              <!-- 添加按钮 -->
              <el-button
                type="success"
                size="mini"
                icon="el-icon-plus"
                title="添加spu属性"
                @click="addSku(row)"
              ></el-button>
              <!-- 编辑按钮 -->
              <el-button
                type="warning"
                size="mini"
                icon="el-icon-edit"
                title="编辑spu属性"
                @click="showSpuForm(row)"
              ></el-button>
              <!-- 显示信息按钮 -->
              <el-button
                type="info"
                size="mini"
                icon="el-icon-info"
                title="显示spu信息"
                style="margin: 0 7px"
                @click="handler(row)"
              ></el-button>
              <!-- 删除按钮 -->

              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="deleteSpu(row)"
              >
                <el-button
                  type="danger"
                  size="mini"
                  icon="el-icon-delete"
                  title="删除spu属性"
                  slot="reference"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          align="center"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[3, 5, 8]"
          :page-size="limit"
          layout="prev, pager, next, jumper,  sizes, total"
          :total="total"
        >
        </el-pagination>
      </div>
      <!-- 添加属性 or 修改属性组件 -->
      <div v-show="isShow == 2">
        <SpuForm @hideSpuForm="hideSpuForm" ref="spuform"></SpuForm>
      </div>
      <!-- 添加sku -->
      <div v-show="isShow == 3">
        <SkuForm ref="sku" @changeScenes="changeScenes" />
      </div>
    </el-card>
    <!-- 对话框 -->
    <el-dialog
      :title="`${spu.spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
    >
      <el-table :data="skuList" style="width: 100%" border v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="width">
        </el-table-column>
        <el-table-column prop="price" label="价格" width="width">
        </el-table-column>
        <el-table-column prop="weight" label="重量" width="width">
        </el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{ row, $index }">
            <img
              :src="row.skuDefaultImg"
              alt=""
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>


<script>
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";
export default {
  name: "Spu",
  components: { SpuForm, SkuForm },
  data() {
    return {
      cateGoryId: {},
      records: [],
      page: 1,
      limit: 5,
      total: 0,
      //控制显示与隐藏
      isShow: 1,
      spu: {},
      skuList: [], //存储的是SKU列表的数据
      dialogTableVisible: false, //控制对话框显示和隐藏
      loading: true,
    };
  },
  methods: {
    //发送请求函数
    async getSpuInfo() {
      let result = await this.$API.spu.reqSpuList(
        this.page,
        this.limit,
        this.cateGoryId.cateGory3Id
      );
      if (result.code == 200) {
        this.records = result.data.records;
        this.total = result.data.total;
      }
    },
    //自定义回调，用来传id
    getCategoryId(cateGoryId) {
      //因为传的是一个对象，所以要深拷贝,这样才能不相互影响
      this.cateGoryId = { ...cateGoryId };
      if (cateGoryId.cateGory3Id) {
        this.getSpuInfo();
      }
    },
    //每页显示最大数发生变化事触发
    handleSizeChange(pageSize) {
      this.limit = pageSize;
      this.getSpuInfo();
    },
    //页码发生变化的时候触发
    handleCurrentChange(page) {
      this.page = page;
      this.getSpuInfo();
    },
    //添加Spu
    addSpu() {
      this.$refs.spuform.addSpuData(this.cateGoryId.cateGory3Id);
      console.log(this.cateGoryId.cateGory3Id, "父组件3ID");
      this.isShow = 2;
    },
    //自定义事件回调
    hideSpuForm({ isShow, flag }) {
      this.isShow = isShow;
      if (flag == "修改") {
        this.getSpuInfo();
      } else {
        this.page = 1;
        this.getSpuInfo();
      }
    },
    //编辑按钮
    showSpuForm(row) {
      this.isShow = 2;
      this.$refs.spuform.initSpuData(row);
    },
    //删除按钮回调
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id);
      if (result.code == 200) {
        if (this.records.length < 2) {
          this.page = this.page - 1;
        }
        this.$message("删除成功");
        this.getSpuInfo();
      }
    },
    // 添加sku
    addSku(row) {
      this.$refs.sku.getData(
        this.cateGoryId.cateGory1Id,
        this.cateGoryId.cateGory1Id,
        row
      );
      this.isShow = 3;
      console.log(row);
    },
    //SkuForm自定义事件
    changeScenes(isShow) {
      this.isShow = isShow;
    },
    async handler(spu) {
      console.log(spu);
      //点击这个按钮的时候，对话框可见的
      this.dialogTableVisible = true;
      //保存spu信息
      this.spu = spu;
      //获取sku列表的数据进行展示
      let result = await this.$API.spu.reqSkuList(spu.id);
      if (result.code == 200) {
        this.skuList = result.data;
        //loading隐藏
        this.loading = false;
      }
    },

    //关闭对话框的回调
    close(done) {
      //loading属性再次变为真
      this.loading = true;
      //清除sku列表的数据
      this.skuList = [];
      //管理对话框
      done();
    },
  },
};
</script>


<style scoped>
</style>